home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / rj.arc / RJEXEC.C < prev   
Encoding:
C/C++ Source or Header  |  1988-06-05  |  4.3 KB  |  144 lines

  1. /****************************************************************/
  2. /*                                */
  3. /* rjexec.c    06/19/86                    */
  4. /*                                */
  5. /* this is a remote job executer for the Novell networking    */
  6. /* system.  it accepts command lines from other stations and    */
  7. /* executes them.  it sends a message back to the station    */
  8. /* that sent the job when it is finished.            */
  9. /*                                */
  10. /* rev 1.1  07/11/86  put tell_others in a separate routine    */
  11. /*                                */
  12. /****************************************************************/
  13.  
  14. #define DELAY 1
  15.  
  16. #include <string.h>
  17. #include "novell.h"
  18.  
  19. main()
  20. {
  21.     int user_no;
  22.     int result, value, users;
  23.     unsigned long handle;
  24.     char station_list[32];
  25.     int i;
  26.     int station;
  27.     char cmnd_line[128], message[128];
  28.     long ltime, target_time;
  29.     char c;
  30.  
  31.     printf("Remote job execution program  --  Ver. 1.1\n");
  32.     printf("For Novell Netware\n");
  33.     printf("Copyright (c) 1986  --  Ken Hales\n\n");
  34.  
  35.     tell_others();
  36.  
  37.     printf("Waiting for a remote command (press 's' to stop this program)...\n");
  38.  
  39.     /********************************************************/
  40.     /* this is an endless loop unless the operator aborts    */
  41.     /********************************************************/
  42.  
  43.     while(1) {
  44.  
  45.         /************************************************/
  46.         /* open a message pipe for each of up to 32     */
  47.         /* users.  this is done within the loop so that    */    
  48.         /* anyone who has just logged in may get in on    */
  49.         /* the action.                    */
  50.         /************************************************/
  51.  
  52.         for (i=0; i<32; station_list[i] = ++i) ;
  53.         pipopen(32, station_list);
  54.  
  55.         /************************************************/
  56.         /* see if a message is here yet                 */
  57.         /************************************************/
  58.  
  59.         pipread(message);
  60.         
  61.         /************************************************/
  62.         /* if one is here, execute it.                 */
  63.         /* otherwise, wait DELAY seconds before looping.*/
  64.         /* check for control-C during wait.        */
  65.         /************************************************/
  66.  
  67.         if (message[0] != 0) {
  68.             for (i=0; i<message[1]; i++) {
  69.                 cmnd_line[i] = message[i+2];
  70.             }
  71.             cmnd_line[i] = '\0';
  72.             printf("Job from station %d: %s\n", message[0], cmnd_line);
  73.             result = system(cmnd_line);
  74.             printf("Job from station %d complete.\n\n", message[0]);
  75.             sprintf(cmnd_line, "Remote job complete.  Result = %d", result);
  76.             station_list[0] = message[0];
  77.             brdcast(1, station_list, cmnd_line);
  78.             printf("Waiting for a remote command (press 's' to stop this program)...\n");
  79.         } else {
  80.             time(<ime);
  81.             target_time = ltime + DELAY;
  82.             while(ltime < target_time) {
  83.                 if (kbhit()) {
  84.                     c = getch();
  85.                     if ((c == 'S') || (c == 's')) {
  86.                         printf("Program has been stopped.\n");
  87.                         semclose(handle);
  88.                         exit(0);
  89.                     } else {
  90.                         printf("\007");
  91.                     }
  92.                 } else {
  93.                     time(<ime);
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
  99.  
  100. /****************************************************************/
  101. /* this routine is used to tell other users which station is    */
  102. /* running this program.  for lack of a better method, we use    */
  103. /* Novell's semaphore routines to accomplish this.  the only    */
  104. /* problem with this is that the semaphore gets closed any time    */
  105. /* a process is terminated, and we have to re-open the         */
  106. /* semaphore.                            */
  107. /****************************************************************/
  108.  
  109. tell_others()
  110. {
  111.  
  112.     int user_no;
  113.     int result, value, users;
  114.     unsigned long handle;
  115.  
  116.     /********************************************************/
  117.     /* find out what user i am                */
  118.     /********************************************************/
  119.  
  120.     user_no = getusr();
  121.  
  122.     /********************************************************/
  123.     /* open semaphore, set to user number               */
  124.     /********************************************************/
  125.  
  126.     result = semopen("rj", user_no, &handle, &users);
  127.  
  128.     if (result != 0) {
  129.         printf("Problem opening semaphore: %d\n", result);
  130.         printf("Program aborted\n");
  131.         exit(1);
  132.     }
  133.  
  134.     result = semexam(handle, &value, &users);
  135.  
  136.     if (value != user_no) {
  137.         printf("Another station is already running this program.\n");
  138.         printf("Program aborted\n");
  139.         semclose(handle);
  140.         exit(1);
  141.     }
  142.  
  143. }
  144.